package mainpk;

import java.io.IOException;

import javax.microedition.rms.InvalidRecordIDException;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordListener;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreFullException;
import javax.microedition.rms.RecordStoreNotFoundException;
import javax.microedition.rms.RecordStoreNotOpenException;

public class Disc implements RecordListener {

	private RecordStore rs = null;
	private RecordEnumeration re = null;
	private String RecStore;
	private int[] ids;

	public Disc(String RecStore){
		this.RecStore = RecStore;
						
		try {
			this.rs = RecordStore.openRecordStore(RecStore, true);
			System.out.println("Records:" + this.rs.getNumRecords());
			this.re = rs.enumerateRecords(null, null, false);
			this.rs.addRecordListener(this);
			this.updateIds();
		} catch (RecordStoreFullException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (RecordStoreNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (RecordStoreException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void setRecordListener(RecordListener rl){
		this.rs.addRecordListener(rl);
	}

	public int add(Recordable rec){
		int id = 0;
		try {
			id = this.rs.addRecord(rec.getBytes(), 0, rec.getBytes().length);
		} catch (RecordStoreNotOpenException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (RecordStoreFullException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (RecordStoreException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return id;
	}

	public void read(int id, Recordable rec){
		byte[] record = null;

		try {
			record = this.rs.getRecord(id);
		} catch (RecordStoreNotOpenException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (RecordStoreException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		try {
			rec.setBytes(record);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void delete(int id){
		try {
			rs.deleteRecord(id);
		} catch (RecordStoreNotOpenException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvalidRecordIDException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (RecordStoreException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public String getRecStore() {
		return RecStore;
	}

	public int getNumRecords(){
		int numRecs = 0;
		try {
			numRecs = rs.getNumRecords();
		} catch (RecordStoreNotOpenException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return numRecs;
	}
	
	private void updateIds(){
		this.ids = new int[this.getNumRecords()];
		int count = 0;
		re.rebuild();
		
		while(re.hasNextElement()){
			try {
				ids[count] = re.nextRecordId();
			} catch (InvalidRecordIDException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			count++;
		}
	}
	
	public int[] getIds(){
		return this.ids;
	}
	
	public int getIdFromIndex(int index){
		return this.ids[index];
	}

	public void recordAdded(RecordStore arg0, int arg1) {
		this.updateIds();
		
	}

	public void recordChanged(RecordStore arg0, int arg1) {
		// TODO Auto-generated method stub
		
	}

	public void recordDeleted(RecordStore arg0, int arg1) {
		this.updateIds();
	}

}
